package com.example.isoftservice.service.impl;

import com.example.isoftservice.mapper.StoreMapper;
import com.example.isoftservice.pojos.Store;
import com.example.isoftservice.service.StoreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassNmae:StoreServiceImpl
 * Descriptiopn:
 *
 * @Create 2023/11/1 15:29
 * @Author yidao
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Resource
    StoreMapper storeMapper;

    @Override
    public Map<String, Object> searchStoreInfo(Store store) {
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(store.getPage(), store.getRows());
        List<Map<String, Object>> list = storeMapper.select(store);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        //获取分页后的信息
        map.put("code", 0);
        map.put("meaaage", "成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());

        return map;
    }

    @Override
    public List searchStoreId() {
        return storeMapper.selectStoreId();
    }

    @Override
    public Map<String, Object> searchProductInfo() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = storeMapper.selectProduct();
        map.put("code", 0);
        map.put("message", "成功");
        map.put("data", list);
        return map;
    }

    @Override
    public Map<String, Object> searchAddressInfo() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = storeMapper.selectAddress();
        map.put("code", 0);
        map.put("message", "成功");
        map.put("data", list);
        return map;
    }

    @Override
    public Map<String, Object> removeStoreById(Store store) {
        HashMap<String, Object> map = new HashMap<>();
        Integer count = storeMapper.delete(store);
        if (count >= 1) {
            map.put("flag", 0);
            map.put("code", true);
            map.put("message", "删除成功");
        } else {
            map.put("flag", 1);
            map.put("code", false);
            map.put("message", "删除失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> addStoreInfo(Store store) {
        HashMap<String, Object> map = new HashMap<>();
        Integer count = this.storeMapper.insert(store);
        if (count == 1) {
            map.put("flag", 0);
            map.put("code", true);
            map.put("message", "添加成功");
        } else {
            map.put("flag", 1);
            map.put("code", false);
            map.put("message", "添加失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> modifyStoreInfo(Store store) {
        HashMap<String, Object> map = new HashMap<>();
        Integer count = this.storeMapper.update(store);
        if (count >= 1) {
            map.put("flag", 0);
            map.put("code", true);
            map.put("message", "更新成功");
        } else {
            map.put("flag", 1);
            map.put("code", false);
            map.put("message", "更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> addProductInfo(Store store) {
        List<Store> list = storeMapper.selectNumber(store);
        System.out.println("==========="+list);
//        Integer existNumber =0;
//        Integer maxNumber=0;
//        Double measurement=0.0;
//        if(list.size() != 0) {
           Integer  existNumber = list.get(0).getStoreExistNumber();
           Integer  maxNumber = list.get(0).getStoreMaxNumber();
           Double  measurement = list.get((0)).getMeasurement();
//        }
        System.out.println("+++++++"+existNumber+"======="+maxNumber+"======");
        Integer number = store.getNumber();
        boolean numberBool = (maxNumber > existNumber + measurement * number) ? true : false;
        HashMap<String, Object> map = new HashMap<>();
        if(numberBool){
            Integer measurement1 = measurement.intValue();
            store.setStoreExistNumber(number*measurement1+existNumber);
            storeMapper.update(store);
            Integer count = this.storeMapper.insertProduct(store);
        if (count == 1) {
            map.put("flag", 0);
            map.put("code", true);
            map.put("message", "添加成功");

        } else {
            map.put("flag", 1);
            map.put("code", false);
            map.put("message", "添加失败");
        }}
        else{
            map.put("flag", 1);
            map.put("code", false);
            map.put("message", "添加失败,超出了仓库最大的容量");
        }
        return map;
    }

    @Override
    public Map<String, Object> modifyProductNumber(Store store) {
        HashMap<String, Object> map = new HashMap<>();
        List<Store> list = storeMapper.selectNumber(store);
        Integer number = list.get(0).getNumber();
        store.setNumber(number+store.getNumber());
        Integer count = this.storeMapper.updateProductNumber(store);
        if (count >= 1) {
            map.put("flag", 0);
            map.put("code", true);
            map.put("message", "更新成功");
        } else {
            map.put("flag", 1);
            map.put("code", false);
            map.put("message", "更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> searchStoreNumber() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String,Object>> list =storeMapper.selectStoreNumber();
        map.put("code",true);
        map.put("data",list);
        return map;
    }


}


